/**
 * 作者: wgx
 * 日期: 2024-10-30 11:38:46
 * 备注: 用户城镇信息表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserCityInfo;
import com.motu.monstercity.protocol.Common.PBUserData;
import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.base.CommonUtils;
import com.motu.monstercity.server.game.commondata.bag.ItemId;
import com.motu.monstercity.server.game.commondata.maintask.MainTaskConstant;
import com.motu.monstercity.server.game.log.MotuLogManager;
import com.motu.monstercity.server.game.logic.*;
import com.motu.vertx.module.utility.toolset.TimeUtils;
import com.motu.vertx.module.utility.toolset.Tool;

public class UserCityInfo extends UserCityInfoModel {
    public UserCityInfo() {
        super();
    }
    public void reset() {
        boolean isUpdate = false;
        if (Tool.isEmpty(getCityName())) {
            super.putCityName(AllParam.INIT_NAME_CITY);
            isUpdate = true;
        }

        if (isUpdate) {
            update();
        }
    }

    // 记录日志并增加人口
    public void addPeasonNum(PBUserData.Builder pbUserData, UserInfo userInfo, int num, Object... params) {
        long balance = getPeasonNum();// 当前余额
        long dbid = 0;
        MotuLogManager.logItemIncome(userInfo, ItemId.PERSON, dbid, balance, num, params);
        super.addPeasonNum(num);
        MainTaskManager.addTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_240, num);// 更新主线任务,成就,日常任务的进度
        RankActManager.addTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_240, num);
    }

    public void addPeasonMax(PBUserData.Builder pbUserData, UserInfo userInfo, int value) {
        super.addPeasonMax(value);
        if (value > 0) {
            MainTaskManager.updateTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_244, getPeasonMax());// 更新主线任务,成就,日常任务的进度
            RankActManager.updateTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_244, getPeasonMax());
        }
    }


    // 有居所的居民的数量 当前兵力 = 有居所的居民 - 伤员（未进行恢复的伤员+正在恢复的伤员） - 行军事件正在占用的兵力
    public int getHousePeasonNum() {
        int peasonNum = getPeasonNum();// 人口数量（居民/无居者）
        int houseNum = getPeasonMax();// 居民数量上限
        if (peasonNum > houseNum) {
            return houseNum;
        } else {
            return peasonNum;
        }
    }


    // 当前兵力 = 有居所的居民 - 伤员（未进行恢复的伤员+正在恢复的伤员) - 行军事件正在占用的兵力
    public int getCurSoldier() {
        UserSoldierRecover userSoldierRecover = BuildManager.getUserSoldierRecover(getId());
        int totalWound = getSoldierWound() + userSoldierRecover.getSoldierTotal();
        if (getHousePeasonNum() > totalWound + getSoldierBattle()) {
            return getHousePeasonNum() - totalWound - getSoldierBattle();
        } else {
            return 0;
        }
    }

    public PBUserCityInfo.Builder takeInitBuilder() {
        PBUserCityInfo.Builder sb = super.takeInitBuilder();
        sb.setSoldier(getCurSoldier());
        return sb;
    }

    public PBUserCityInfo.Builder takeBuilder() {
        PBUserCityInfo.Builder sb = super.takeBuilder();
        sb.setSoldier(getCurSoldier());
        return sb;
    }
}
